package com.lineage.data.item_etcitem.reel;

import com.lineage.config.Configtype;
import com.lineage.data.cmd.EnchantWeapondiy;
import com.lineage.data.executor.ItemExecutor;
import com.lineage.server.model.Instance.L1ItemInstance;
import com.lineage.server.model.Instance.L1PcInstance;
import com.lineage.server.serverpackets.S_ServerMessage;
import com.lineage.server.serverpackets.S_SystemMessage;
import com.lineage.server.serverpackets.ServerBasePacket;
import java.util.Random;

public class ScrollEnchantArmordiy extends ItemExecutor {
  private int type1;
  
  private int type2;
  
  public static ItemExecutor get() {
    return new ScrollEnchantArmordiy();
  }
  
  public void execute(int[] data, L1PcInstance pc, L1ItemInstance item) {
    int targObjId = data[0];
    L1ItemInstance tgItem = pc.getInventory().getItem(targObjId);
    if (tgItem == null)
      return; 
    if (tgItem.isEquipped()) {
      pc.sendPackets((ServerBasePacket)new S_ServerMessage("(预防误点机制启动)装备中无法强化"));
      return;
    } 
    if (tgItem.isSeal()) {
      pc.sendPackets((ServerBasePacket)new S_SystemMessage(String.valueOf(tgItem.getLogName()) + "处于绑定状态！"));
      return;
    } 
    int safe_enchant = tgItem.getItem().get_safeenchant();
    boolean isErr = false;
    int use_type = tgItem.getItem().getUseType();
    switch (use_type) {
      case 2:
      case 18:
      case 19:
      case 20:
      case 21:
      case 22:
      case 25:
      case 47:
        if (safe_enchant < 0)
          isErr = true; 
        break;
      default:
        isErr = true;
        break;
    } 
    int weaponId = tgItem.getItem().getItemId();
    if (weaponId >= 246 && weaponId <= 255)
      isErr = true; 
    if (tgItem.getBless() >= 128)
      isErr = true; 
    if (tgItem.getItem().getItemId() >= 120444 && tgItem.getItem().getItemId() <= 120448)
      isErr = true; 
    if (tgItem.getItem().getItemId() >= 301 && weaponId <= 310)
      isErr = true; 
    if (tgItem.getItem().getItemId() >= 1040 && weaponId <= 1043)
      isErr = true; 
    if (isErr) {
      pc.sendPackets((ServerBasePacket)new S_ServerMessage(79));
      return;
    } 
    if (tgItem.getEnchantLevel() == Configtype.weaponlv) {
      pc.sendPackets((ServerBasePacket)new S_ServerMessage("您的防具已达上限值。"));
      return;
    } 
    int enchant_level = tgItem.getEnchantLevel();
    EnchantWeapondiy enchantWeapondiy = new EnchantWeapondiy();
    int randomELevel = enchantWeapondiy.randomELevel(tgItem, item.getBless());
    Random random = new Random();
    int rnd = random.nextInt(100) + 1;
    if (enchant_level == this.type1) {
      if (rnd <= this.type2) {
        enchantWeapondiy.successEnchant(pc, tgItem, randomELevel);
        pc.getInventory().removeItem(item, 1L);
      } else {
        enchantWeapondiy.failureEnchant(pc, tgItem);
        pc.getInventory().removeItem(item, 1L);
      } 
    } else {
      pc.sendPackets((ServerBasePacket)new S_ServerMessage("您的防具强化值需等于:+" + this.type1 + "才允许强化。"));
    } 
  }
  
  public void set_set(String[] set) {
    try {
      this.type1 = Integer.parseInt(set[1]);
    } catch (Exception exception) {}
    try {
      this.type2 = Integer.parseInt(set[2]);
    } catch (Exception exception) {}
  }
}
